// pages/chartere_bus6/reserve6-4/reserve6-4.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:{},
    userInfo:{},
    specificationId:0,
    user:{},
    addMoneyImg: '',
    addCouponImg:'',
    addCoupon: '../images/shop-yes.png',
    coupon:{},
    shoppingList: [],
    addMoney:0,
    money:0,
    text:{},
    code:""
  },

  /**
   * 生命周期函数--监听页面加载
   */

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var idArr = options.id.split(",");
    console.log(idArr);
    console.log(options.id);

    var userInfo = util.getKey("userInfo");
    this.setData({
      userInfo: userInfo
    })
   var pages = getCurrentPages();

      var prevPage = pages[pages.length - 2];  //上一个页面
    this.getShoppingList(idArr);
    httpUtil.getHttp("/search/get/text"
      , { id: 8 }, function (res) {
        //将markdown内容转换为towxml数据
        let data = app.towxml.toJson(res.text, 'markdown');
        that.setData({
          text: data
        })
      })

  } , getShoppingList: function (list) {
    var that = this;
    var userInfo = util.getKey("userInfo");

    httpUtil.postHttp("/order/getShoppingList?userId=" + userInfo.id 
      , {list:list}, function (res) {
        var money=0;
        for(var i=0;i<res.length;i++){
          var model=res[i];
          for (var s = 0; s < model.specificationList.length;s++){
            var da = model.specificationList[s];
            money += (parseFloat(da.money) * parseFloat(da.num));
          }
        }
         that.setData({
           shoppingList: res,
           money: money
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  Carhelp:function(){
    wx.navigateTo({
      url: '../add_inf/add_inf'
    })
  },
  lookMore: function () {
    wx.navigateTo({
      url: '../answer/answer'
    })
  },
  selContact: function () {
    wx.addPhoneContact({
      success: function (e) {
        console.log(e);
      }
    })
  },
  
  codeSend: function () {
    wx.showToast({
      title: '发送成功',
      icon: 'succes',
      duration: 1000,
      mask: true
    })
  },
  setName: function (e) {
    var that = this;
    console.log(that.data.user)
    that.data.user.name = e.detail.value;
  },
  setPinYinName: function (e) {
    var that = this;
    console.log(that.data.user)

    that.data.user.pinYinName = e.detail.value;


  }, setTel: function (e) {
    var that = this;
    that.data.user.tel = e.detail.value;

  }, setEmail: function (e) {
    var that = this;
    that.data.user.eMail = e.detail.value;

  }, setSpareTel: function (e) {
    var that = this;
    that.data.user.spareTel = e.detail.value;

  }, setWx: function (e) {
    var that = this;
    that.data.user.wx = e.detail.value;
  },
  setCode: function (e) {
    var that = this;
    that.data.code = e.detail.value;
  }, addMoney: function () {
    var that = this;
     this.setData({
      addMoneyImg: that.data.addMoneyImg == '' ? '../images/shop-yes.png' : '',
      addMoney: that.data.addMoneyImg == '' ? 56 : 0,
      money: that.data.addMoneyImg == '' ? parseFloat(parseFloat(that.data.money) + 56).toFixed(2) : parseFloat(parseFloat(that.data.money) - 56).toFixed(2)
    })
  },
  codeSend: function (e) {
    //获取列表
    var that = this;

    if (that.data.code == '') {
      wx.showToast({
        title: '请输入优惠码',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    httpUtil.getHttp("/coupon/get/" + that.data.code + "/code", {}, function (coupon) {
      if (coupon == null || coupon == '') {
        wx.showToast({
          title: '优惠卷不存在',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      } if (coupon.status == 0) {
        wx.showToast({
          title: '优惠卷未领取',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      } if (coupon.status == 2) {
        wx.showToast({
          title: '优惠卷已使用',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      }
      if (coupon.startStatus == 1) {
        wx.showToast({
          title: '优惠卷未生效',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      }
      if (that.data.money < coupon.total) {
        wx.showToast({
          title: '未达到满减条件',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      }

      var money = parseFloat(that.data.money) - coupon.num <= 0 ? 0.01 : parseFloat(that.data.money) - coupon.num
      if (money == null || money == NaN) {
        return;
      }
       that.setData({
        coupon: coupon,
        money: money
      })
    })

  },
  addCouponImg: function () {
    var that = this;
    this.setData({
      addCouponImg: '../images/shop-yes.png'
    })
    this.setData({
      addCoupon: '',
    })
  },
  addCoupon: function () {
    var that = this;
    console.log("进入");
    this.setData({
      addCoupon: '../images/shop-yes.png'
    })
    this.setData({
      addCouponImg: '',
    })


  }, paySuccess: function () {
    var that = this;

    var user = that.data.user;
    console.log(that.data.user);
    console.log(user.name);
    console.log(user.name=='');

    if (user.name == '' || user.name==undefined) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }
    if (user.pinYinName == '' || user.name == undefined) {
      wx.showToast({
        title: '请填写姓名拼音',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;

      if (user.eMail == '' || user.name == undefined) {
        wx.showToast({
          title: '请填写邮箱',
          icon: 'loading',
          duration: 1000,
          mask: true
        })
        return;
      }
    }
    if (user.tel == '' || user.name == undefined) {
      wx.showToast({
        title: '请填写手机号',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return;
    }

    var userInfo = util.getKey("userInfo");
    var travelMoney = 0;
    if (that.data.addMoneyImg != '') {
      travelMoney = 56;
    }
    var code = that.data.coupon.code;
    if (code == undefined) {
      code = "";
    }
    var data = { name: user.name, pinYinName: user.pinYinName, eMail: user.eMail, tel: user.tel, spareTel: user.spareTel, wx: user.wx, list: that.data.shoppingList  }
    httpUtil.postHttp("/order/pay/shopping?userId=" + userInfo.id  + "&couponCode=" + code + "&travelMoney=" + travelMoney 
      , data, function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            wx.navigateTo({
              url: '../paySuccess/paySuccess'
            })
          },
          'fail': function (res) {
            console.log(res);
            wx.showToast({
              title: "支付失败",
              icon: 'loading',
              duration: 500
            });

          }
        })
      })

   
  },
  })